package com.example.smartcitydemo.ui.subway.subway.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.viewModels
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.R
import com.example.smartcitydemo.base.BaseFragment
import com.example.smartcitydemo.bean.SubWayNoticeBean
import com.example.smartcitydemo.databinding.FragmentSubwayNoticeBinding
import com.example.smartcitydemo.ext.init
import com.example.smartcitydemo.ui.subway.SubWayNoticeAdapter
import com.example.smartcitydemo.viewModel.SubwayViewModel.SubWayViewModel

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.ui.subway.fragment
 * @NAME: Caleb.F
 * @DATE: 2021/11/17 15:13
 * @Email: 1919796283@qq.com
 */
class SubwayNoticeFragment :
    BaseFragment<FragmentSubwayNoticeBinding>(R.layout.fragment_subway_notice) {
    private val subWayNoticeAdapter: SubWayNoticeAdapter by lazy {
        SubWayNoticeAdapter().apply {
            init()
            setDiffCallback(SubWayNoticeBean.diffCallback)
        }
    }

    private val subwayViewModel: SubWayViewModel by viewModels()

    override fun initData() {
        subwayViewModel.getSubWayNotice()
    }

    override fun initView(bindView: FragmentSubwayNoticeBinding, savedInstanceState: Bundle?) {
        bindView.run {
            toolbarSubwayNotice.tvContent.text = "地铁通知"
            subwayNoticeRecycler.adapter = subWayNoticeAdapter
        }
    }

    override fun addObservers(bindView: FragmentSubwayNoticeBinding) {
        subwayViewModel.subwayNoticeData.observe(viewLifecycleOwner) {
            Log.d(TAG, "addObservers: $it")
            when(it){
                is Resource.Success ->
                    subWayNoticeAdapter.setDiffNewData(it.data!!.toMutableList())
                is Resource.Failure ->
                    subWayNoticeAdapter.setEmptyView(R.layout.error_layout)
                is Resource.Loading->
                    subWayNoticeAdapter.setEmptyView(R.layout.loading_layout)
            }

        }
    }

}